import myRequest from '../request.js'

import dicts from '@/config/dicts.js'



// 获取购物车中商品库存数
export const cartStock = (id) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/quantity', 'get', { skuId: id }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 创建交易
export const createTransaction = (val) => {
  return new Promise((resolve, reject) => {
    // myRequest(dicts.buyer + '/purchaser/trade/carts/create/trade', 'post', val, { 'Content-type': 'application/json' }).then(res => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/deal', 'post', val, { 'Content-type': 'application/json' }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 计算订单结算金额  
export const calculateAmount = (id) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/trade/preview/calculation', 'get', { couponId: id }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 核对订单 金额
export const checkOrderPrice = (couponId) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/trade/preview/calculation' + couponId, 'get').then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 核对订单
export const checkOrder = () => {
  return new Promise((resolve, reject) => {
    // myRequest(dicts.buyer + '/purchaser/trade/carts/trade/preview', 'get').then(res => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/inspect', 'get').then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 更新单个商品的状态
export const singleSelectedStatus = (id, val) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/sku/checked/' + id, 'post', val).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 更新商品选中状态
export const selectedAllStatus = (val) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/sku/checked', 'post', val).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 更新商品选中状态
export const selectedStatus = (skuId, checked) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/trade/carts/sku/checked/${skuId}`, 'post', { checked: checked }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 更新购物车数量
export const toUpdateCarNum = (a, b) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/sku/num/' + a, 'post', b).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 清空购物车
export const emptyCart = () => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts', 'delete').then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 删除购物车商品
export const deleteCarGoods = (val) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/sku/remove', 'delete', val).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 获取购物车详情
export const getCarInfo = () => {
  return new Promise((resolve, reject) => {
    // myRequest(dicts.buyer + '/purchaser/trade/carts/all', 'get').then(res => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/groups', 'get').then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 获取购物车数量
export const getCartNum = () => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/trade/carts/count', 'get').then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 获取支付方式列表
export const getPayMode = () => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/payment/method', 'get').then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 支付
export const payment = (paymentMethod, paymentClient, val) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/payment/cashier/pay/${paymentMethod}/${paymentClient}`, 'get', val).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 线下转账 提醒
export const transferAccountsRemind = (paymentMethod, paymentClient, data) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/payment/cashier/pay/${paymentMethod}/${paymentClient}`, 'put', data).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}
// 查询会员是否有推广订单
export const getMemberExtensionStatus = () => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + '/purchaser/order/order/popularize/check', 'get').then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
// 获取组合中的商品列表  
export const getCombinationProducts = (portfolioSkuId) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/goods/portfolio/${portfolioSkuId}/sku`, 'get').then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}


// 获取当前会员的是否可一键领取订单满赠活动
export const getGiftReceiveStatus = (presentActivityId) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/promotion/present/canReceive/${presentActivityId}`, 'get').then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
// 获取当前会员的订单满赠规则列表
export const getGiftReceiveList = () => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/promotion/present/getPresents/list`, 'get').then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
// 会员一键领取订单满赠活动
export const giftReceive = (presentActivityId) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/promotion/present/receive/${presentActivityId}`, 'get').then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
/**
 * /purchaser/payment/method/suitable/methods 合适的支付方式
 * /purchaser/payment/method/appropriate/methods  聚合支付列表
 */
export const paySuitable = (val) => {
  return new Promise((resolve, reject) => {
    // myRequest(dicts.buyer + `/purchaser/payment/method/suitable/methods`, 'get', val).then(res => {
    // myRequest(dicts.buyer + `/purchaser/payment/method/appropriate/methods`, 'get', val).then(res => {
    myRequest(dicts.buyer + `/purchaser/payment/method/opportune/methods`, 'get', val).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export const paySuitable1 = (val) => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/payment/method/suitable/methods`, 'get', val).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}



// 购物车可展示的优惠券列表
export const displayableCoupon = () => {
  return new Promise((resolve, reject) => {
    myRequest(dicts.buyer + `/purchaser/trade/carts/difference/coupons`, 'get').then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}